import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        homeTabIndex: 0,
        categorybtnindex: 0,
        FloorCardList: [],
        isAdd: false,
        showcartlist: [],
        showcartlistitem: {},
        showcartlistitemlist: [],
        done: true,
        totalcost: 0,
        istotalClick: true,
        // isshopClick: true,
        atleastonegood: false,
        goodnumber: 0,
        checkbtncssflag: true,
        activepasswordtype: "password",
        hasnotlogin: true,
        show: false,
        index: 0,
        componentId:"introduce"
    },

    mutations: {
        componentId(state,obj){
            state.componentId=obj
        },
        tab(state, index) {
            state.index = index
        },
        attention(state) {
            state.show = !state.show

        },
        homeTabIndexChange: function homeTabIndexChange(state, index) {
            state.homeTabIndex = index;
        },
        catenavchange(state, index) {
            state.categorybtnindex = index;
        },
        FloorCardList(state, array) {
            state.FloorCardList = array
        },
        addtocart(state, obj) {
            state.isAdd = true;
            state.done = false
            if (state.showcartlist.length == 0) {
                state.showcartlistitem.shopname = obj.shopname
                state.showcartlistitem.choosed = obj.selected
                state.showcartlistitemlist.push(obj)
                state.showcartlistitem.list = state.showcartlistitemlist
                state.showcartlistitemlist = []
                state.showcartlist.push(state.showcartlistitem)
                state.showcartlistitem = {}
                state.done = true
            } else {
                for (let i = 0; i < state.showcartlist.length; i++) {
                    if (obj.shopname !== state.showcartlist[i].shopname) {
                        state.done = false

                    } else {
                        for (let j = 0; j < state.showcartlist[i].list.length; j++) {
                            if (obj.desc !== state.showcartlist[i].list[j].desc) {
                                state.done = false
                            } else {
                                state.showcartlist[i].list[j].count++
                                state.done = true
                                return
                            }
                        }
                        if (state.done == false) {
                            state.showcartlist[i].list.push(obj)
                            state.done = true
                            return
                        }
                    }
                }
                if (state.done == false) {
                    state.showcartlistitem.shopname = obj.shopname
                    state.showcartlistitem.choosed = obj.selected
                    state.showcartlistitemlist.push(obj)
                    state.showcartlistitem.list = state.showcartlistitemlist
                    state.showcartlistitemlist = []
                    state.showcartlist.push(state.showcartlistitem)
                    state.showcartlistitem = {}
                    state.done = true
                    return
                }
            }


        },
        empty(state) {
            state.isAdd = false
        },
        calculate(state) {
            state.atleastonegood = false
            state.goodnumber = 0
            state.totalcost = 0
            state.istotalClick = true
            for (let j = 0; j < state.showcartlist.length; j++) {
                for (let k = 0; k < state.showcartlist[j].list.length; k++) {
                    if (state.showcartlist[j].list[k].selected == true) {
                        state.atleastonegood = true
                        state.goodnumber += state.showcartlist[j].list[k].count
                        state.totalcost += (state.showcartlist[j].list[k].count) * (state.showcartlist[j].list[k].price)
                    } else {
                        state.istotalClick = false
                    }
                }
            }
        },
        editcart(state) {
            state.istotalClick = !state.istotalClick;
            if (state.istotalClick == true) {
                for (let j = 0; j < state.showcartlist.length; j++) {
                    state.showcartlist[j].choosed = true
                    for (let k = 0; k < state.showcartlist[j].list.length; k++) {
                        state.showcartlist[j].list[k].selected = true
                    }
                }
            } else {
                if (state.istotalClick == false) {
                    for (let j = 0; j < state.showcartlist.length; j++) {
                        state.showcartlist[j].choosed = false
                        for (let k = 0; k < state.showcartlist[j].list.length; k++) {
                            state.showcartlist[j].list[k].selected = false
                        }
                    }
                }
            }
        },
        totalselect(state) {
            state.istotalClick = !state.istotalClick
            if (state.istotalClick == true) {
                for (let j = 0; j < state.showcartlist.length; j++) {
                    state.showcartlist[j].choosed = true
                    for (let k = 0; k < state.showcartlist[j].list.length; k++) {
                        state.showcartlist[j].list[k].selected = true
                    }
                }
            } else {
                if (state.istotalClick == false) {
                    for (let j = 0; j < state.showcartlist.length; j++) {
                        state.showcartlist[j].choosed = false
                        for (let k = 0; k < state.showcartlist[j].list.length; k++) {
                            state.showcartlist[j].list[k].selected = false
                        }
                    }
                }
            }

        },
        shopselect(state, index) {
            if (state.showcartlist[index].choosed == true) {
                for (let k = 0; k < state.showcartlist[index].list.length; k++) {
                    state.showcartlist[index].list[k].selected = true
                }
            } else {
                for (let k = 0; k < state.showcartlist[index].list.length; k++) {
                    state.showcartlist[index].list[k].selected = false
                }
            }
        },
        goodselect(state, index) {
            for (let k = 0; k < state.showcartlist[index].list.length; k++) {
                if (state.showcartlist[index].list[k].selected == false) {
                    state.showcartlist[index].choosed = false
                    return
                }
            }

            state.showcartlist[index].choosed = true

        },
        eyetab(state) {
            state.checkbtncssflag = !state.checkbtncssflag
            if (state.checkbtncssflag == true) {
                state.activepasswordtype = "password"
            }
            if (state.checkbtncssflag == false) {
                state.activepasswordtype = "text"
            }
        },
        haslogin(state) {
            state.hasnotlogin = !state.hasnotlogin
        },


    }
});

export default store;